<?php

namespace IDock\Insurance\JDAL\Response;

use IDock\Http\Response;
use IDock\Printer\PremiumUnderwritingPrinter;

class UnderwritingResponse extends AllJDALResponse
{

    /**
     * @var PremiumUnderwritingPrinter $printer
     */
    public $printer = PremiumUnderwritingPrinter::class;

    public function printerInit(): Response
    {
        $data = $this->getBody();

        $this->printer->inquiryNo = $data['basePart']['applicationRef'] ?? '';

        //商业险状态
        $commHandlerResultCode = $data['ruleHandlerResult']['commHandlerResultCode'] ?? '';
        $commHandlerResultMsg = $data['ruleHandlerResult']['commHandlerResultMsg'] ?? '';
        //交强险状态
        $tplHandlerResultCode = $data['ruleHandlerResult']['tplHandlerResultCode'] ?? '';
        $tplHandlerResultMsg = $data['ruleHandlerResult']['tplHandlerResultMsg'] ?? '';

        if (!(float)$commHandlerResultCode and !(float)$tplHandlerResultCode) {
            $this->printer->statusCode = 'U1';
            return $this;
        }

        if (in_array('0001',[$commHandlerResultCode,$tplHandlerResultCode])) {
            $this->printer->statusCode = 'U2';
            $this->printer->messageTip = implode("\r\n",[$commHandlerResultMsg,$tplHandlerResultMsg]);
            return $this;
        }

        if (in_array('0002',[$commHandlerResultCode,$tplHandlerResultCode])) {
            $this->printer->statusCode = 'U3';
            return $this;
        }

        return $this;
    }
}
